﻿using System;
using System.IO;
using System.Xml.Serialization;

namespace objectOutlet.Umbraco.ConnectedPages.Helpers
{
	internal static class Serialization
	{
		private static object DeserializeXmlObject(string xml, Type typeofobject)
		{
			object result;
			using (var reader = new StringReader(xml))
			{
				var serializer = new XmlSerializer(typeofobject);
				result = serializer.Deserialize(reader);
				reader.Close();
			}
			return result;
		}

		internal static object DeserializeXmlObjectFromFile(string filename, Type typeofobject)
		{
			string xml;
			using (var sr = File.OpenText(filename))
			{
				xml = sr.ReadToEnd();
				sr.Close();
			}
			return DeserializeXmlObject(xml, typeofobject);
		}

		internal static void SerializeXmlObjectToFile(Type typeofobject, object o, string filename)
		{
			using (var sw = File.CreateText(filename))
			{
				var serializer = new XmlSerializer(typeofobject);
				serializer.Serialize(sw, o);
				sw.Close();
			}
		}
	}
}